//==============================================================================
// Routines to do initialisation stuff   
//==============================================================================

#define MP_GLOBALS 0
#include "MP.h"

//==============================================================================
// function to initialise global variables
//==============================================================================
extern void __stdcall MP_init_globals()
{ int i;
  static int done=0;
  
     if (done==0)      // Do this bit once only
       { done=1;
         program_finished=0;
         MP_Global_Debug=0;
         MP_Global_scroll_count=20;
         MP_Clear_tags();
         MP_Global_Port[WIFIREMOTE]=8017;                      // wifiRemote default port
         MP_Global_Port[MPEXTENDED]=4322;                      // MPExtended default port
         for (i=0;i<NUM_CONNECTIONS;i++) 
           { sprintf(MP_Global_IPAddress[i].i,"%d.%d.%d.%d",127,0,0,1); // The local machine
             MP_Global_connecting[i]=0; MP_Global_connected[i]=0;       // Flag set to 1 if sockets are connected
           }
         strcpy(MP_Global_wPasscode,"0000");
         strcpy(MP_Global_wUserName,"Guest");
         strcpy(MP_Global_wPassword,"password");                 
         strcpy(MP_Global_mUserName,"admin");
         strcpy(MP_Global_mPassword,"admin");                 
         MP_Global_mWait=0;                 // time between MPExtended calls
         strcpy(MP_Global_Title,"-----");   
         strcpy(MP_Global_ProcessInactive,"0");
         MP_Global_Dump=0;  
         MP_Global_usebars=0;
         strcpy(MP_Global_DateFormat,"%H:%M %d/%m/%y");
         MP_Global_dumpinterval=300;        // Default dump interval 5 mins
         MP_Global_last_request=0;          // last time a request was sent
         MP_Global_authorised=0;            // not authorised yet
         for (i=0;i<MAX_LOG_CACHE;i++) MP_log_cache[i].used=0;
         MP_log_cache_ptr=0;
         MP_Global_mWait=10;                // time between MPExtend updates
         fp_log=NULL;
         MP_Global_last_dump=(double)clock()/CLOCKS_PER_SEC;                // Counter time
     }       
}


//==============================================================================
// function to read from the configuration file
//==============================================================================
extern void __stdcall MP_read_cfg_file(int callnum)
{
     FILE *fp;
     char line[128],str[80],str2[80],mod[80];
     int linelen,i,j,k,l,tag;
//
// Set globals to default values
//
    MP_init_globals();
    strcpy(mod,"MP_read_cfg_file:");
    // Set up the start of the properties string
    strcpy(MP_Global_properties1,          // what properities to request
              "{\"Type\":\"properties\",\"Properties\":[\"#Play.Current.Title\""); 
    strcpy(MP_Global_properties2,          // what properities to request
              "{\"Type\":\"properties\",\"Properties\":[\"#Play.Current.Channel\""); 
              
    MP_logl(3,mod,"Started");        
//
// open the configuration file then read each line looking for specific names and store the values
//
     fp=fopen("plugins\\MP.cfg","r");
     if (!fp) MP_errlog(mod,"Failed to open cfg file");
     MP_logl(3,mod,"File Opened");
     if (fp != NULL) 
      { while (fgets(line,128,fp))
         { strcpytonl(line,line);      // remove \n character
           linelen=strlen(line);
           if (linelen>0) 
            { if ((line[0]!='/') && (line[0]!='\r') && (line[0]!='\n') && (line[0]!=' ')) 
              { if (!strncmp(line,"Debug=",6)) sscanf(&line[6],"%d",&MP_Global_Debug);       
                else if (!strncmp(line,"Dump=",5)) sscanf(&line[5],"%d",&MP_Global_Dump);  
                else if (!strncmp(line,"WifiRemote_IPAddress=",21)) sscanf(&line[21],"%s",MP_Global_IPAddress[WIFIREMOTE].i);                 
                else if (!strncmp(line,"MPExtended_IPAddress=",21)) sscanf(&line[21],"%s",MP_Global_IPAddress[MPEXTENDED].i);                 
                else if (!strncmp(line,"WifiRemote_Port=",16)) sscanf(&line[16],"%d",&MP_Global_Port[WIFIREMOTE]);  
                else if (!strncmp(line,"MPExtended_Port=",16)) sscanf(&line[16],"%d",&MP_Global_Port[MPEXTENDED]);  
                else if (!strncmp(line,"WifiRemote_Passcode=",20)) sscanf(&line[20],"%s",MP_Global_wPasscode); 
                else if (!strncmp(line,"WifiRemote_UserName=",20)) sscanf(&line[20],"%s",MP_Global_wUserName); 
                else if (!strncmp(line,"WifiRemote_Password=",20)) sscanf(&line[20],"%s",MP_Global_wPassword);
                else if (!strncmp(line,"MPExtended_UserName=",20)) sscanf(&line[20],"%s",MP_Global_mUserName); 
                else if (!strncmp(line,"MPExtended_Password=",20)) sscanf(&line[20],"%s",MP_Global_mPassword);
                else if (!strncmp(line,"MPExtended_Wait=",16)) sscanf(&line[16],"%d",&MP_Global_mWait);
                else if (!strncmp(line,"UseBars=",8)) sscanf(&line[8],"%d",&MP_Global_usebars);
                else if (!strncmp(line,"ProcessInactive=",16)) sscanf(&line[16],"%s",&MP_Global_ProcessInactive); 
                else if (!strncmp(line,"DateFormat=",11)) sscanf(&line[11],"%s",MP_Global_DateFormat);
                else if (!strncmp(line,"DumpInterval=",13)) sscanf(&line[13],"%d",&MP_Global_dumpinterval);
                else if (!strncmp(line,"Scrollrate=",11)) sscanf(&line[11],"%d",&MP_Global_scroll_count);
                // Xlate="instring","Outstring"
                else if (!strncmp(line,"Xlate=",6)) 
                  { i=strfind(line,'"',6); j=strfind(line,'"',i); 
                    k=strfind(line,'"',j); l=strfind(line,'"',k);
                    MP_xlate_store(&line[i],&line[k],j-i-1,l-k-1);
                    strcpy(str,"Translate string \""); strncat(str,&line[i],j-i-1); 
                    strcat(str,"\" to \""); strncat(str,&line[k],l-k-1); strcat(str,"\"");
                    MP_logl(3,mod,str);
                  }
                else if (!strncmp(line,"Properties=",11))  
                  { if (strlen(MP_Global_properties1)>MAX_PROPS_LENGTH)
                      { strcat(MP_Global_properties2,",\"#");  
                        strcat(MP_Global_properties2,&line[11]);
                        strcat(MP_Global_properties2,"\"");
                      }
                    else
                      { strcat(MP_Global_properties1,",\"#");  
                        strcat(MP_Global_properties1,&line[11]);
                        strcat(MP_Global_properties1,"\"");
                      }
                    str2[0]='\0';
                    strcpy(str,"Properties."); strcat(str,&line[11]);
                    if (callnum==1) i=MP_put_tag(str, 1, 0, str2);
                    MP_logl(3,"MP_read_cfg_file: Defined ",str);
                  }
                else MP_errlog("MP_Init Error - Line not processed:",line);
              } 
            }
         }
        fclose(fp);
      }
     // Close the properties string
     strcat(MP_Global_properties1, "]}\r\n\r\n");
     strcat(MP_Global_properties2, "]}\r\n\r\n");

     // set up any other tag values which require initialisation
     MP_put_tag("IsRecording",1,1,"false");
     MP_put_tag("NRecordings", 1, 0, "None");
     MP_put_tag("Recordings", 1, 0, "None");
     MP_put_tag("IRecordings", 0, 0, "0");
     MP_put_tag("Position", 0, 0, "0");
     MP_put_tag("Duration", 0, 0, "0");

     // log the cfg file settings
     sprintf(str,"DumpInterval=%d",MP_Global_dumpinterval); MP_logl(3,mod,str);     
     sprintf(str,"MP_Global_dump=%d",MP_Global_Dump); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_IPAddress=%s",MP_Global_IPAddress); MP_logl(3,mod,str); 
     for (i=0;i<NUM_CONNECTIONS;i++) { sprintf(str,"MP_Global_Port[%d]=%d",i,MP_Global_Port[i]); MP_logl(3,mod,str); }
     sprintf(str,"MP_Global_wPasscode=%s",MP_Global_wPasscode); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_wUserName=%s",MP_Global_wUserName); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_wPassword=%s",MP_Global_wPassword); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_mUserName=%s",MP_Global_mUserName); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_mPassword=%s",MP_Global_mPassword); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_mWait=%d",MP_Global_mWait); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_usebars=%d",MP_Global_usebars); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_ProcessInactive=%s",MP_Global_ProcessInactive); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_DateFormat=%s",MP_Global_DateFormat); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_dumpinterval=%d",MP_Global_dumpinterval); MP_logl(3,mod,str); 
     sprintf(str,"MP_Global_scroll_count=%d",MP_Global_scroll_count); MP_logl(3,mod,str); 
     MP_logl(3,mod,"Finished");        
}



